#include"device.h"

static int dev_id_min =1000; 
static int count = 0;
/*
* @brief 内置的ID生成器,新建设备时,根据全局dev_id_min 
* 
*  @return int 
*    
*获取设备号号
*/
int generate_id()
{
	return dev_id_min++;
}


/**
 *@brief 从文件中读取数据到程序中,形成数据链表
 * 
 *@return node_t
 */
//加载设备函数
node_t * load_dev()
{ 
	//1.创建链表，并检检查
	node_t *devlist  = 	create_dlist();
	SYSERR(NULL,==,devlist,"declist is null",NULL);

	//2.定义文件并打开文件 并检查
	FILE *devf = fopen("device.dat","r");
	SYSERR(NULL,==,devf,"open device error",NULL);
	rewind(devf);
	device_t temp ={0};
	//循环 用fscanf()读出每个设备数据
	while(fscanf(devf,"%d %d %d %f %s %s %s %f\n",
				&temp.dev_id,&temp.dev_order,&temp.dev_status,
				&temp.cur_value,temp.dev_name,
				temp.dev_type,temp.dev_ip,&temp.power_ele) == 8)
	{
		temp.dev_id = generate_id();
		temp.dev_order = count++;
		//用头部插入函数将数据插入链表
		int rel = insert_hdlist(devlist,&temp,sizeof(device_t));
		memset(&temp,0,sizeof(device_t));
	}


	fclose(devf);
	//返回链表地址
	return devlist;
	
}


//保存设备函数
int save_dev(node_t * head)
{
	//检查链表设备数据链表
	SYSERR(NULL,==,head,"head is null",-1);

	//打开要存入的文件，并检查是否打开成功
	FILE *fp = fopen("device.dat","w");
	SYSERR(NULL,==,fp,"fp open error",-1);

	//边循环边输入数据到文件
	node_t *temp = head->after;
	device_t tempdata = {0};
	
	while(temp != head)
	{
		if(NULL == temp->data)
		{
			temp = temp->after;
			continue;
		}
		memcpy(&tempdata,temp->data,sizeof(device_t));
		fprintf(fp,"%d %d %d %f %s %s %s %f\n",
				tempdata.dev_id,tempdata.dev_order,tempdata.dev_status,
				tempdata.cur_value,tempdata.dev_name,
				tempdata.dev_type,tempdata.dev_ip,tempdata.power_ele);

		temp = temp->after;
	}
	//关闭文件
	fclose(fp);
	//销毁链表释放内存
	destroy_dlist(&head);
	//成功返回0
	return 0;
}

int cmpDevid(const void *data1,const void *data2)
{
	SYSERR(NULL,==,data1,"data1 is null",-1);
	SYSERR(NULL,==,data2,"data2 is null",-1);

	device_t *p1 = (device_t*)data1;
	device_t *p2 = (device_t*)data2;

	return p1->dev_id - p2->dev_id;

}
//	添加设备函数
int add_dev(node_t * head, const device_t * newdev)
{
	//检查设备链接地址和设备数据地址是不NULL，
	SYSERR(NULL,==,head,"head is null",-1);
	SYSERR(NULL,==,newdev,"newdev is null",-1);
	//头部插入到设备链表
	device_t *temp = (device_t*)newdev;
	temp->dev_id = generate_id();
	temp->dev_order = count++;
	insert_hdlist(head,temp,sizeof(device_t));
	//按设备号排序
	blubSort_dlist(head,cmpDevid);
	//成返回0
	return 0;
}

int cmpDviceid(const void *data1,const void *data2)
{
	device_t *p1 = (device_t*)data1;
	device_t *p2 = (device_t*)data2;

	return p1->dev_id - p2->dev_id;

}

int cmpDviceorder(const void *data1,const void *data2)
{
	device_t *p1 = (device_t *)data1;
	device_t *p2 = (device_t *)data2;

	return p1->dev_order - p2->dev_order;

}
int cmpDvicestatus(const void *data1,const void *data2)
{
	device_t *p1 = (device_t *)data1;
	device_t *p2 = (device_t *)data2;

	return p1->dev_status - p2->dev_status;

}

int cmpDvicename(const void *data1,const void *data2)
{
	device_t *p1 = (device_t *)data1;
	device_t *p2 = (device_t *)data2;

	return strcmp(p1->dev_name,p2->dev_name);

}

int cmpDvicetype(const void *data1,const void *data2)
{
	device_t *p1 = (device_t *)data1;
	device_t *p2 = (device_t *)data2;

	return strcmp(p1->dev_type,p2->dev_type);

}

int cmpDviceip(const void *data1,const void *data2)
{
	device_t *p1 = (device_t *)data1;
	device_t *p2 = (device_t *)data2;

	return strcmp(p1->dev_ip,p2->dev_ip);

}


node_t *find_dev(node_t * head, const device_t *data,cmpfun_t cmpfun)
{

    // 检查
	SYSERR(NULL,==,head,"head is null",NULL);
	SYSERR(NULL,==,data,"data is null",NULL);

	//创建链表，并检检查
	node_t *newdevlist  = 	create_dlist();
	SYSERR(NULL,==,newdevlist,"newdeclist is null",NULL);
    node_t *temp = head;
	while((temp=find_vdlist(temp,data,cmpfun)) != NULL )
	{

		insert_hdlist(newdevlist,temp->data,sizeof(device_t));

	}

	return newdevlist; 

}


//删除设备函数 (按设备号，按设备名字，按设备类型)
int del_dev(node_t * head, const device_t * dev,cmpfun_t cmpfun)
{
	//检查
	SYSERR(NULL,==,head,"head is null",-1);
	SYSERR(NULL,==,dev,"dev is null",-1);
	//创建一个节点接收查找到的设备节点
	node_t *temp =head;
	while((temp = delete_vdlist(temp,dev,cmpfun)) != NULL)
		;
	if(NULL == temp)
		return 0;
	else
		return -1;

}
//修改全部
int updatedev_all(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	int id = p1->dev_id;
	*p1 = *p2;
	p1->dev_id = id;
	return 0;
}

//修改设备序号 
int updatedev_order(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	p1->dev_order = p2->dev_order;
	return 0;
}

//修改设备当前值 
int updatedev_value(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	p1->cur_value = p2->cur_value;
	return 0;
}

//修改设备状态 
int updatedev_status(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	p1->dev_status = p2->dev_status;
	return 0;
}

//修改设备状态 
int updatedev_power(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	p1->power_ele = p2->power_ele;
	return 0;
}

//修改名字
int updatedev_name(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	memcpy(p1->dev_name,p2->dev_name,sizeof(p1->dev_name));
	return 0;
}

//修改类型
int updatedev_type(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	memcpy(p1->dev_type,p2->dev_type,sizeof(p1->dev_type));
	return 0;
}

//修改IP
int updatedev_ip(void *old_data,const void *new_data)
{

	SYSERR(NULL,==,old_data,"old_data is null",-1);
	SYSERR(NULL,==,new_data,"new_data is null",-1);
	device_t *p1 = (device_t *)old_data;
	device_t *p2 = (device_t *)new_data;

	memcpy(p1->dev_ip,p2->dev_ip,sizeof(p1->dev_ip));
	return 0;
}


//修改设备
node_t *update_dev(node_t * head,int id,const device_t * updatedev,updatefun_t updatefun)
{
	//检查
	SYSERR(NULL,==,head,"head is null",NULL);
	SYSERR(NULL,==,updatedev,"updatedev is null",NULL);
	//按id号查找找设备，
	node_t *temp = head;
	device_t data ={.dev_id = id};
	node_t *oldev = find_vdlist(head,&data,cmpDviceid);

	//将找到的设备和新设备放入修改函数，修改
	updatefun(oldev->data,updatedev);
	//修改成功返回设备链接头
	return oldev;
}

//设备排序
int sort_dev(node_t * head, cmpfun_t cmpfun)
{
	//检查
	SYSERR(NULL,==,head,"head is null",-1);
	int rel = blubSort_dlist(head,cmpfun);
	return rel;
}




void showdev_all(const void *devdata)
{
	device_t *data = (device_t*)devdata;
	printf("ID:%5d 设备序号:%5d  设备状态:%5d 设备当前值:%5f 设备名称:%5s  设备类型:%5s   设备IP:%13s  设备功率:%5f\t\n",
			data->dev_id,data->dev_order,data->dev_status,
			data->cur_value,data->dev_name,
			data->dev_type,data->dev_ip,data->power_ele);			
}


//显示设备函数
void show_dev(node_t *head, showfun_t showfun)
{
	//检查
	SYSERR(NULL,==,head,"head is null",);
	show_hdlist(head,showfun);
}


